package org.jmh.auth.service.impl;

import org.jmh.auth.dto.LoginDTO;
import org.jmh.auth.service.ILoginService;
import org.jmh.auth.vo.LoginVO;
import org.jmh.base.context.LoginContext;
import org.jmh.org.domain.Employee;
import org.jmh.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.UUID;

@Service
public class ILoginServiceImpl implements ILoginService {
    @Autowired
    private IEmployeeService service;
    @Override
    public LoginVO login(LoginDTO loginDTO) {
        //先根据账号查对象
        Employee employee = service.selectByName(loginDTO.getAccount());
        if(Objects.isNull(employee)){
            throw new RuntimeException("登陆失败，再好好想想");
        }
        //判断对象中的密码是否匹配
        if(!employee.getPassword().equals(loginDTO.getCheckPass()))
            throw new RuntimeException("不对不对再想想");
        //如果匹配返回token
        String token = UUID.randomUUID().toString();
        LoginVO loginVO = new LoginVO(token, employee);
        LoginContext.loginMap.put(token,employee);
        //封装VO 返回
        return loginVO;
    }
}
